package main

import (
	//"fmt"
	//"net/http"
	"log"
	"net/http"
	"serialServer/utility"
	"serialServer/mysqlUtility"
	"serialServer/controller"
	"serialServer/serialUtility"
)


func main() {
	// 初始化数据库
	var mysqlConfig, errMySql = utility.GetMysqlConfig()
	if errMySql != nil {
		panic(errMySql.Error())
	}
	mysqlUtility.ConnectToDB(mysqlConfig)
	mysqlUtility.DBConn.SetMaxOpenConns(100)

	// 初始化serial port
	//var serialConfig, errSerial = utility.GetSerialConfig()
	//if errSerial != nil {
	//	panic(errSerial.Error())
	//}
	//serialUtility.ConnectToSerialPort(serialConfig)

	// 开始读取serial port的数据

	// 暂时先用一个mock
	go serialUtility.ReadDataToDbMock()

	// 开始http服务
	http.HandleFunc("/getLatestDataByDeviceId", controller.GetLatestByDeviceId) //设置访问的路由
	http.HandleFunc("/getRecentDataByDeviceIdAndStartDatetimeAndCount", controller.GetRecentByDeviceIdAndStartDatetimeAndCount) //设置访问的路由
	fs := http.FileServer(http.Dir("."))
	http.Handle("/", http.StripPrefix("/", fs))
	err := http.ListenAndServe(":8080", nil) //设置监听的端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
//
